// 绘图
// Created by denglibin on 2021/2/5.
//
#include <SDL.h>
#include <stdio.h>
#define WIN_WIDTH  1366
#define WIN_HEIGHT  768
static SDL_Window * win;
static SDL_Surface * winSurface;
static SDL_Renderer * winRenderer;
static int quit = 0;

static void init(){

}

/**
 * 创建并显示窗口
 */
static void creatWindow(){
    win = SDL_CreateWindow("demo2", SDL_WINDOWPOS_UNDEFINED,
                           SDL_WINDOWPOS_UNDEFINED,
                           WIN_WIDTH, WIN_HEIGHT,
                           SDL_WINDOW_SHOWN);
    if(win == NULL){
        printf("SDL_CreateWindow ERROR:%s\n", SDL_GetError());
        exit(1);
    }
    //获取窗口显示层
    winSurface = SDL_GetWindowSurface(win);
    //主窗口渲染器
    winRenderer = SDL_CreateRenderer(
            win,
            -1,  //序号 -1表示第一个
            SDL_RENDERER_SOFTWARE);
}

/**
 * 填充背景
 */
static void fillBg(){
    SDL_SetRenderDrawColor(winRenderer, 0,128, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderFillRect(winRenderer, NULL);
}
/**
 * 绘制矩形
 */
static void drawRect(){
    SDL_Rect rect = {0, 0, 200, 200};
    SDL_SetRenderDrawColor(winRenderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderDrawRect(winRenderer, &rect);
}
/**
 * 填充矩形
 */
static void fillRect(){
    SDL_Rect rect = {0, 210, 200, 200};
    SDL_SetRenderDrawColor(winRenderer, 255, 255, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderFillRect(winRenderer, &rect);
}
/**
 * 绘制直线
 */
static void drawLine(){
    SDL_SetRenderDrawColor(winRenderer, 255, 255, 128, SDL_ALPHA_OPAQUE);
    SDL_RenderDrawLine(winRenderer, 0, 420, 400, 420);
}

/**
 * 绘制多条线
 */
static void drawLines(){
    static SDL_Point points[5] = {
            {0, 500},
            {500, 500},
            {500, 600},
            {700, 700},
            {700, 600}};
    SDL_SetRenderDrawColor(winRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderDrawLines(winRenderer, points, 5);
}
/**
 * 绘制
 */
static void draw(){
    SDL_RenderClear(winRenderer);
    fillBg(); //填充背景
    drawRect(); //绘制矩形
    fillRect(); // 填充矩形
    drawLine(); //直线
    drawLines(); //多条线
    // show window
    SDL_RenderPresent(winRenderer);
}
/**
 * 事件处理
 * @param e
 */
static void keyDownEvent(SDL_Event* e){
    switch (e->key.keysym.sym) {
        case SDLK_ESCAPE:
            quit = 1;
            break;
        case SDLK_DOWN:
            break;
        case SDLK_UP:
            break;
        default:
            break;
    }

}
/**
 * 事件循环
 */
static void eventLoop(){
    SDL_Event  event;
    while (!quit)
    {//主消息循环
        //SDL_WaitEvent(&event); //阻塞
        SDL_PollEvent(&event); //将队列头中的事件抛出来。
        switch (event.type)
        {
            //用户从菜单要求退出程序
            case SDL_QUIT:
                quit = 1;
                break;
            case SDL_KEYDOWN:
                keyDownEvent(&event);
                break;
            default:
                break;
        }
        //draw(); //绘制
        SDL_Delay(20);
    }
}
/**
 * 关闭
 */
static void close(){
    // 清理资源
    //SDL_DestroyTexture(bgTexture);
    SDL_FreeSurface(winSurface);
    SDL_DestroyRenderer(winRenderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
}

/**
 * 允许程序
 */
static void run(){
    creatWindow();
    draw();
    eventLoop();
    close();
}

int main7(int argc, char* argv[]){
    init();
    run();
    return 0;
}
